package com.joker.orange.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.joker.orange.domain.*;
import com.joker.orange.domain.bo.PushCaricatureBo;
import com.joker.orange.domain.bo.PushNovelBo;
import com.joker.orange.domain.bo.PushVideoBo;
import com.joker.orange.domain.bo.VideoInfoBo;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 接收数据推送
 */
@RequiredArgsConstructor
@Service
public class AcceptPushedDataService {

    private final ILongVideoCategoryService longVideoCategoryService;
    private final IVideoInfoService videoInfoService;
    private final INovelCategoryService novelCategoryService;
    private final INovelInfoService novelInfoService;
    private final INovelChapterService novelChapterService;
    private final ICaricatureCategoryService caricatureCategoryService;
    private final ICaricatureInfoService caricatureInfoService;
    private final ICaricatureChapterService caricatureChapterService;
    private final IAnimeCategoryService animeCategoryService;

    /**
     * 接收视频数据
     */
    @Async
    public void acceptPushVideo(JSONObject grid, String type) {
        PushVideoBo videoBo = grid.toBean(PushVideoBo.class);
        Long categoryId = 0L;
        if ("0".equals(type)) {
            LongVideoCategory longVideoCategoryVo = longVideoCategoryService.queryByName(videoBo.getCategory());
            if (longVideoCategoryVo != null) {
                categoryId = longVideoCategoryVo.getId();
            }
        } else if ("2".equals(type)) {
            AnimeCategory animeCategory = animeCategoryService.queryByName(videoBo.getCategory());
            if (animeCategory != null) {
                categoryId = animeCategory.getId();
            }
        } else {
            categoryId = 1L;
        }

        if (categoryId != 0L) {
            VideoInfoBo videoInfo = new VideoInfoBo();
            videoInfo.setCategoryId(categoryId);
            videoInfo.setVideoType(type);
            videoInfo.setVideoUrl(videoBo.getRpath() + "/index.m3u8");
            videoInfo.setVideoCover(videoBo.getOutput().getPic1());
            videoInfo.setVideoName(videoBo.getTitle());
            videoInfo.setVideoTime(new BigDecimal(videoBo.getMetadata().getTime()));
            videoInfoService.insertByBo(videoInfo);
        }
    }

    /**
     * 接收漫画数据
     */
    @Async
    public void acceptPushCaricature(JSONArray jsonArray) {
        List<PushCaricatureBo> caricatureInfoList = jsonArray.toList(PushCaricatureBo.class);
        for (PushCaricatureBo bo : caricatureInfoList) {
            List<CaricatureChapter> caricatureChapterList = new ArrayList<>();
            CaricatureCategory caricatureCategory = caricatureCategoryService.queryByCategoryName(bo.getType());
            if (caricatureCategory == null) {
                continue;
            }
            CaricatureInfo caricatureInfo = getCaricatureInfo(bo, caricatureCategory);
            Long caricatureId = caricatureInfoService.insertCaricatureInfo(caricatureInfo);
            for (PushCaricatureBo.Chapter chapter : bo.getChapterList()) {
                CaricatureChapter caricatureChapter = new CaricatureChapter();
                caricatureChapter.setCaricatureId(caricatureId);
                caricatureChapter.setChapterUrl(String.join(",", chapter.getPicList()));
                caricatureChapter.setChapterTitle(chapter.getName());
                caricatureChapterList.add(caricatureChapter);
            }
            caricatureChapterService.insertBatch(caricatureChapterList);
        }
    }

    private static CaricatureInfo getCaricatureInfo(PushCaricatureBo bo, CaricatureCategory novelCategory) {
        CaricatureInfo caricatureInfo = new CaricatureInfo();
        caricatureInfo.setCategoryId(novelCategory.getId());
        caricatureInfo.setCaricatureCover(bo.getCover());
        caricatureInfo.setCaricatureName(bo.getName());
        caricatureInfo.setCurrentChapter(bo.getSubTitle());
        caricatureInfo.setUpdateChapter(bo.getSubTitle());
        caricatureInfo.setChapterTotal(String.valueOf(bo.getChapterCount()));
        caricatureInfo.setCaricatureIntroduce(bo.getIntroduction());
        caricatureInfo.setCaricatureLabel(bo.getTags());
        caricatureInfo.setCaricatureType(bo.getSubTitle().contains("连载") ? "0" : "1");
        return caricatureInfo;
    }

    /**
     * 接收小说数据
     */
    @Async
    public void acceptPushNovel(JSONArray jsonArray) {

        List<PushNovelBo> pushNovelList = jsonArray.toList(PushNovelBo.class);
        for (PushNovelBo novelBo : pushNovelList) {
            List<NovelChapter> novelChapterList = new ArrayList<>();
            NovelCategory novelCategory = novelCategoryService.queryByName(novelBo.getType());
            if (novelCategory == null) {
                continue;
            }
            NovelInfo novelInfo = getNovelInfo(novelBo, novelCategory);
            Long novelId = novelInfoService.insertNovelInfo(novelInfo);
            for (PushNovelBo.Chapter chapter : novelBo.getChapterList()) {
                NovelChapter novelChapter = new NovelChapter();
                novelChapter.setNovelId(novelId);
                novelChapter.setChapterContent(chapter.getContent());
                novelChapter.setChapterTitle(chapter.getName());
                novelChapterList.add(novelChapter);
            }
            novelChapterService.insertBatch(novelChapterList);
        }

    }

    private static NovelInfo getNovelInfo(PushNovelBo novelBo, NovelCategory novelCategory) {
        NovelInfo novelInfo = new NovelInfo();
        novelInfo.setCategoryId(novelCategory.getId());
        novelInfo.setNovelCover(novelBo.getCover());
        novelInfo.setNovelName(novelBo.getName());
        novelInfo.setCurrentChapter(novelBo.getSubTitle());
        novelInfo.setUpdateChapter(novelBo.getSubTitle());
        novelInfo.setChapterTotal(String.valueOf(novelBo.getChapterCount()));
        novelInfo.setNovelIntroduce(novelBo.getIntroduction());
        novelInfo.setNovelLabel(novelBo.getTags());
        novelInfo.setNovelType(novelBo.getSubTitle().contains("连载") ? "0" : "1");
        return novelInfo;
    }

    public static String removeExtension(String filePath) {
        int dotIndex = filePath.lastIndexOf('.');
        if (dotIndex > 0) {
            return filePath.substring(0, dotIndex);
        }
        return filePath;  // 如果没有找到扩展名，返回原始字符串
    }



}
